package dialog;

import android.app.DatePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import com.example.zx.theline.R;

import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import base.BaseDialog;

/**
 * Created by zx on 2017/4/26.
 */

/**
 * 暂时没有使用
 * */
public class SearchDialog extends BaseDialog {

    TextView sgrq;
    EditText rjhh;
    AutoCompleteTextView xgbm;

    TextView queding;
    TextView quxiao;

    onSearchListener mOnSearchListener;

    public SearchDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_search);
        InitView();
        InitData();
        InitListen();
    }

    @Override
    protected void InitView() {
        sgrq= (TextView) findViewById(R.id.sgrq);
        rjhh= (EditText) findViewById(R.id.rjhh);
        xgbm= (AutoCompleteTextView) findViewById(R.id.xgbm);
        queding= (TextView) findViewById(R.id.queding);
        quxiao= (TextView) findViewById(R.id.quxiao);
    }

    @Override
    protected void InitData() {

    }

    @Override
    protected void InitListen() {
        sgrq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                                String[] date={};
                String mYear;
                String mMonth;
                String mDay;
                //如果textview中存在日期 获取该日期显示在datapickdialog上
                if(isDate(sgrq.getText().toString()))
                {
                    date=sgrq.getText().toString().split("-");
                }
                //data为空 jcrq框内没有数据
                if(date.length<1){
                    mYear= String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
                    mMonth= String.valueOf(Calendar.getInstance().get(Calendar.MONTH));
                    mDay= String.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
                } else {
                    mYear=date[0];
                    mMonth=date[1];
                    mDay=date[2];
                }
                new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        sgrq.setText(year+"-"+(month+1)+"-"+dayOfMonth);
                    }
                },Integer.parseInt(mYear) ,Integer.parseInt(mMonth)-1,Integer.parseInt(mDay)).show();


            }
        });
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnSearchListener==null){
                    return;
                }
                mOnSearchListener.onSearchDate(sgrq.getText().toString(), rjhh.getText().toString(), xgbm.getText().toString());
            }
        });
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SearchDialog.this.dismiss();
            }
        });
    }

    //判断字符串 是否为日期
    public boolean isDate(String s){
        boolean flag;
        String reg="([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})";
        Pattern pattern=Pattern.compile(reg);
        Matcher matcher=pattern.matcher(s);
        flag=matcher.matches();
        return flag;
    }

    public void setOnSearchListener(onSearchListener mOnSearchListener){
        this.mOnSearchListener=mOnSearchListener;
    }

    public interface  onSearchListener{
        void   onSearchDate(String SearchDate,String SearchJhh,String Xgbm);
    }
}
